/**
 * 
 */
package leetCode;

import java.util.ArrayList;

/**
 * @author zhongfang
 *
 */
public class ExcelSheetColumnTitle {
	public static String convertToTitle(int n) {
		StringBuilder sb = new StringBuilder();
		ArrayList<Integer> resInt = new ArrayList<>();
		while (n != 0) {
			int x = n % 26;
			if (x == 0) {
				x = 26;
				n -= 26;
			}
			resInt.add(x);
			n /= 26;
		}
		int size = resInt.size();
		for (int i = size - 1; i >= 0; i--) {
			sb.append((char) ('A' + resInt.get(i) - 1));
		}
		return sb.toString();
	}

	public static String convertToTitle1(int n) {
		StringBuilder sb = new StringBuilder();
		ArrayList<Integer> resInt = new ArrayList<>();
		n--;
		while (n != 0) {
			resInt.add(n % 26);
			n /= 26;
		}
		System.out.println(resInt);
		int size = resInt.size();
		for (int i = size - 1; i >= 0; i--) {
			sb.append((char) ('A' + resInt.get(i) - 1));
		}
		return sb.toString();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		for (int i = 1; i < 100; i++) {
			System.out.println(convertToTitle(i));

		}
	}

}
